package com.jrx.anytxn.customer.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.jrx.anytxn.common.data.TxnRespCode;
import com.jrx.anytxn.common.exception.TxnBizException;
import com.jrx.anytxn.common.uitl.BeanMapping;
import com.jrx.anytxn.customer.constant.CustomerConstant;
import com.jrx.anytxn.customer.dto.customer.LimitAdjustSummary;
import com.jrx.anytxn.customer.dto.customer.TlBatchAdjustLimitRes;
import com.jrx.anytxn.customer.entity.CmCustomerFirstLevel;
import com.jrx.anytxn.customer.entity.CmCustomerSecondLevel;
import com.jrx.anytxn.customer.entity.CmLimitAdjustLog;
import com.jrx.anytxn.customer.mapper.ext.ExtCmCustomerFirstLevelMapper;
import com.jrx.anytxn.customer.mapper.ext.ExtCmCustomerSecondLevelMapper;
import com.jrx.anytxn.customer.mapper.ext.ExtCmLimitAdjustLogMapper;
import com.jrx.anytxn.customer.service.IAdjustListSummaryService;
import com.jrx.anytxn.i18n.response.TxnPage;
import com.jrx.anytxn.param.dto.sms.PrSmsSendingRulesTableRes;
import com.jrx.anytxn.param.entity.BtLimitAdjustInfo;
import com.jrx.anytxn.param.entity.TlBatchAdjustLimit;
import com.jrx.anytxn.param.mapper.ext.ExtBtLimitAdjustInfoMapper;
import com.jrx.anytxn.param.mapper.ext.ExtTlBatchAdjustLimitMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

@Service
public class AdjustListSummaryServiceImpl implements IAdjustListSummaryService {

    private static Logger logger = LoggerFactory.getLogger(AdjustListSummaryServiceImpl.class);

    @Resource
    private ExtBtLimitAdjustInfoMapper extBtLimitAdjustInfoMapper;
    @Resource
    private ExtCmLimitAdjustLogMapper extCmLimitAdjustLogMapper;
    @Resource
    private ExtTlBatchAdjustLimitMapper extTlBatchAdjustLimitMapper;
    @Resource
    private ExtCmCustomerFirstLevelMapper extCmCustomerFirstLevelMapper;
    @Resource
    private ExtCmCustomerSecondLevelMapper extCmCustomerSecondLevelMapper;

    @Override
    public TxnPage<TlBatchAdjustLimitRes> findAllPageByTenantId(Integer pageNum, Integer pageSize, String tenantId) throws TxnBizException {
        Page<PrSmsSendingRulesTableRes> page = PageHelper.startPage(pageNum, pageSize, null);

        List<TlBatchAdjustLimit> tlBatchAdjustLimits = extTlBatchAdjustLimitMapper.selectByTenantId(tenantId);
        if (tlBatchAdjustLimits.isEmpty()) {
            logger.error(TxnRespCode.NO_DATA.getMsg());
            throw new TxnBizException(TxnRespCode.NO_DATA.getMsg());
        }
        List<TlBatchAdjustLimitRes> res = BeanMapping.copyList(tlBatchAdjustLimits, TlBatchAdjustLimitRes.class);
        return new TxnPage<>(pageNum, pageSize, page.getTotal(), res);
    }

    @Override
    public List<LimitAdjustSummary> createExcelMessage(String batchId, String tenantId,String fileName) throws TxnBizException {
        logger.info("开始生成批量调额汇总数据,批次编号 =" + batchId);

        List<BtLimitAdjustInfo> btLimitAdjustInfoList = extBtLimitAdjustInfoMapper.selectListByBatchIdAndTenantId(batchId,tenantId);

        List<LimitAdjustSummary> limitAdjustSummaryList = new ArrayList<>();
        //根据seqNo 查询 调额历史表
        for(BtLimitAdjustInfo info : btLimitAdjustInfoList){
            CmLimitAdjustLog log = extCmLimitAdjustLogMapper.selectBySeqNoAndCusId(info.getTenantId(),info.getCustomerId(),info.getSeqNo());

            LimitAdjustSummary summary = new LimitAdjustSummary();
            summary.setCustomerId(log.getCustomerId());
            CmCustomerFirstLevel cmCustomerFirstLevel = extCmCustomerFirstLevelMapper.selectFirstLevelByOrgIdAndCusId(log.getCustomerId(), log.getOrganizationId(), log.getTenantId());
            if(null != cmCustomerFirstLevel){
                summary.setChineseName(cmCustomerFirstLevel.getChineseName());
                summary.setIdNumber(cmCustomerFirstLevel.getIdNumber());
            }

            //查询客户二级信息
            CmCustomerSecondLevel secondLevel = extCmCustomerSecondLevelMapper.selectByCustomerIdAndChannel(log.getCustomerId(),log.getChannel(),log.getTenantId());
            if(null != secondLevel){
                summary.setMobile(secondLevel.getMobile());
            }

            summary.setCreateTime(log.getCreateTime());
            summary.setAdjustType("批量调额");
            summary.setLimitPermBefore(log.getLimitPermBefore()==null? BigDecimal.ZERO:log.getLimitPermBefore());
            summary.setLimitPermAfter(log.getLimitPermAfter()==null?BigDecimal.ZERO:log.getLimitPermBefore());
            summary.setFileName(fileName);
            String result = log.getResult().equals(CustomerConstant.FAIL) ? "失败":"成功";
            summary.setResult(result);
            summary.setUpdateBy(log.getUpdateBy());

            limitAdjustSummaryList.add(summary);
        }

        return limitAdjustSummaryList;
    }
}